package com.tbit.main.config.datasource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.tbit.main.config.datasource.properties.DruidProperties;
import com.tbit.main.config.mybatis.MybatisPlusConfig;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @author chenzhuang
 */
@Configuration
@MapperScan(basePackages = "com.tbit.main.dao.ter", sqlSessionFactoryRef = "terSqlSessionFactory")
public class TerSource {
    /**
     * 返回ter数据库的数据源
     *
     * @return
     */
    @Bean(name = "terDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.ter")
    public DataSource dataSource(DruidProperties druidProperties) {
        return druidProperties.dataSource(DruidDataSourceBuilder.create().build());
    }

    @Bean(name = "terMybatisProperties")
    @ConfigurationProperties(prefix = "mybatis-plus.ter")
    public MybatisPlusProperties mybatisProperties() {
        return new MybatisPlusProperties();
    }

    /**
     * 返回ter数据库的会话工厂
     *
     * @param ds
     * @return
     * @throws Exception
     */
    @Bean(name = "terSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("terDataSource") DataSource ds, @Qualifier("terMybatisProperties") MybatisPlusProperties mybatisProperties) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(ds);
        bean.setMapperLocations(mybatisProperties.resolveMapperLocations());
        bean.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackage());
        /**
         * opt: spring-boot-starter减少无用的配置提示(不兼容改动,调整了com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties.configuration类型)
         * https://github.com/baomidou/mybatis-plus/releases  #v3.5.4
         */
        MybatisConfiguration configuration = new MybatisConfiguration();
        mybatisProperties.getConfiguration().applyTo(configuration);
        bean.setConfiguration(configuration);        bean.setGlobalConfig(mybatisProperties.getGlobalConfig());
        Interceptor[] plugins = {new MybatisPlusConfig().mybatisPlusInterceptor()};
        bean.setPlugins(plugins);
        return bean.getObject();
    }

    /**
     * 返回ter数据库的会话模板
     *
     * @param sessionFactory
     * @return
     */
    @Bean(name = "terSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("terSqlSessionFactory") SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }

    /**
     * 返回ter数据库的事务
     *
     * @param ds
     * @return
     */
    @Bean(name = "terTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("terDataSource") DataSource ds) {
        return new DataSourceTransactionManager(ds);
    }
}
